﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using Microsoft.AspNet.SignalR.Client;

namespace ClientWinForms
{
    public partial class Form1 : Form
    {
        HubConnection hubConnection;
        IHubProxy hubProxy;
        public Form1()
        {
            InitializeComponent();

            hubConnection = new HubConnection("http://localhost:30526/signalr/hubs");
            hubProxy = hubConnection.CreateHubProxy("HelloWorld");
            hubProxy.On("connected", Connected);
            hubProxy.On<string, string>("show", (user, pesan) => Show(user, pesan));
            hubConnection.Start().Wait();
        }

        private void Connected()
        {
            MessageBox.Show("User baru bergabung.");
        }

        private void Show(string user, string pesan)
        {
            this.listBoxChat.Invoke(new MethodInvoker(() => this.listBoxChat.Items.Add(user + " : " + pesan)));
        }

        private void buttonSend_Click(object sender, EventArgs e)
        {
            if (!String.IsNullOrEmpty(textBoxUsername.Text))
            {
                hubProxy.Invoke("Send", textBoxUsername.Text, textBoxPesan.Text).Wait();
            }
            else
            {
                MessageBox.Show("Username belum diisi.");
            }
        }
    }
}
